package com.jinyi.wwdeng.ui.steps.impl

import com.jinyi.base.mvp.BaseModel
import com.jinyi.base.network.IRequestCallback
import com.jinyi.base.network.NetworkUtils
import com.jinyi.wwdeng.network.ApiService
import com.jinyi.wwdeng.network.HttpBaseParams.addCommonParams
import com.jinyi.wwdeng.ui.steps.contract.StepAccountContract
import org.json.JSONObject

/**
 * JinYiTest
 *
 * @Author：wwdeng
 * @CreateTime：2024/4/14 18:42
 * <pre>
 * Model层  接口实现类
</pre> *
 */
class StepAccountModelImpl(iCallBack: StepAccountContract.Presenter) :
    BaseModel<StepAccountContract.Presenter>(iCallBack), StepAccountContract.Model {

    /**
     * 获取已绑定的账户列表
     */
    override fun loadBindAccountListAction() {
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.API_COMMON_URL)
        val params = JSONObject().addCommonParams().apply {
            put("path", ApiService.API_PATH.API_PATH_ACCOUNT_LIST_FRENCH)
        }
        NetworkUtils.post(httpUrl, params, object : IRequestCallback {
            //请求成功
            override fun onRequestSuccess(data: JSONObject) {
                callBack.onBindAccountListSuccess(data)
            }

            //请求失败
            override fun onRequestError(data: JSONObject) {
                callBack.onRequestError(data.optString("errorMsg"))
            }

            //请求完成
            override fun onRequestFinish() {
                callBack.onRequestFinish()
            }
        })
    }

    /**
     * 添加绑定账户
     */
    override fun addBindAccountAction(mobile: String, walletChannel: String, bindFlag: String) {
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.API_COMMON_URL)
        val params = JSONObject().addCommonParams().apply {
            put("path", ApiService.API_PATH.API_PATH_ACCOUNT_ADD)
            put("walletMobile", mobile)
            put("walletMobileConfirm", mobile)
            put("walletChannel", walletChannel)
            put("addBindFlag", bindFlag)
        }
        NetworkUtils.post(httpUrl, params, object : IRequestCallback {
            //请求成功
            override fun onRequestSuccess(data: JSONObject) {
                callBack.addBindAccountSuccess(mobile, data)
            }

            //请求失败
            override fun onRequestError(data: JSONObject) {
                callBack.onRequestError(data.optString("errorMsg"))
            }

            //请求完成
            override fun onRequestFinish() {
                callBack.onRequestFinish()
            }
        })
    }

    /**
     * 删除账户
     */
    override fun deleteAccountAction(data: JSONObject) {
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.API_COMMON_URL)
        val params = JSONObject().addCommonParams().apply {
            put("path", ApiService.API_PATH.API_PATH_ACCOUNT_DELETE)
            put("ids", data.optString("id"))
        }
        NetworkUtils.post(httpUrl, params, object : IRequestCallback {
            //请求成功
            override fun onRequestSuccess(data: JSONObject) {
                callBack.onDeleteAccountSuccess(data)
            }

            //请求失败
            override fun onRequestError(data: JSONObject) {
                callBack.onRequestError(data.optString("errorMsg"))
            }

            //请求完成
            override fun onRequestFinish() {
                callBack.onRequestFinish()
            }
        })
    }
}
